﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Perceiveit.Xml.Converters
{

    #region public class DateTimeFormattedXConverter : FormattableXConverter<DateTime>

    public class DateTimeFormattedXConverter : FormattableXConverter<DateTime>
    {

        public DateTimeFormattedXConverter(string format, IFormatProvider provider)
            : base(format, provider)
        {
        }


        public override DateTime FromString(string value)
        {
            DateTime v;
            if (string.IsNullOrEmpty(value) || !DateTime.TryParseExact(value, this.Format, this.Formatter, System.Globalization.DateTimeStyles.None, out v))
                v = DateTime.MinValue;

            return v;
        }

        public override string ToString(DateTime value)
        {
            if (value == DateTime.MinValue)
                return string.Empty;
            else
                return value.ToString(this.Format, this.Formatter);
        }
    }

    #endregion
}
